package project.pianpian.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import project.pianpian.entity.Order;
import project.pianpian.mapper.DemandMapper;
import project.pianpian.mapper.OrderMapper;
import project.pianpian.entity.Demand;

import project.pianpian.utils.ReturnUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * Description:
 * Param:
 * return:
 * Author: HSY
 * Date:2022/12/26
 */
@RestController
@Slf4j
@RequestMapping("/main")
public class DemandAPI {
    @Autowired
    private DemandMapper demandMapper;
    @Autowired
    private OrderMapper orderMapper;

    @GetMapping("/demands")
    //查待接订单的同时终止超期未完成的order
    public JSONObject getDemands(){
        orderCheck();
        List<Demand> demandList = demandMapper.getDemands();
        JSONObject returnJSON = new JSONObject();
        returnJSON.put("demands",demandList);

        return ReturnUtils.success(200,"获取待接订单成功",returnJSON);
    }

    @GetMapping("/demandsByPage")
    public JSONObject findPage(@RequestParam int pageNum, @RequestParam int pageSize){
        orderCheck();
        int tmpPageNum =(pageNum-1)*pageSize;
        List<Demand> demandList = demandMapper.selectPage(tmpPageNum,pageSize);
        JSONObject returnJSON = new JSONObject();
        returnJSON.put("demands",demandList);
        return ReturnUtils.success(200,"获取待接订单成功",returnJSON);

    }

    @GetMapping("/demands/{id}")
    public JSONObject getDemandById(@PathVariable("id") int id)
    {
        Demand demand = demandMapper.getDemandById(id);
        JSONObject returnJSON = new JSONObject();
        returnJSON.put("demand",demand);
        return ReturnUtils.success(200,"获取需求成功",returnJSON);
    }

    @PostMapping("/demands")
    public JSONObject addDemand(@RequestBody JSONObject jsonObject)
    {
        int isContracted=0;
        Date time = new Date();
        int budget = jsonObject.getInteger("budget");
        Date ddl = jsonObject.getDate("ddl");
        String title = jsonObject.getString("title");
        int employer_id = jsonObject.getInteger("employer_id");
        String describe = jsonObject.getString("describe");
        Demand demand = new Demand(employer_id,budget,title,describe,ddl,isContracted,time);

        int num=demandMapper.addDemand(demand);

        if(num == 1)
            return ReturnUtils.success(200,"需求提交成功");
        else
            return ReturnUtils.fail(500,"需求提交失败");
    }

    @PutMapping("/demands")
    public JSONObject updateDemandStatus(int id)
    {
        int num=demandMapper.updateDemandStatus(id);
        if(num == 1)
            return ReturnUtils.success(200,"需求状态修改成功");
        else
            return ReturnUtils.fail(500,"需求状态修改失败");
    }

    void orderCheck(){
        List<Order> tmp = orderMapper.getOrders();
        for(Order order:tmp){
            int tmpId =order.getId();
            boolean before = order.getDeadline().before(new Date());
            if(before){
                int num = orderMapper.updateOrderStatus(tmpId);
                if(num==0){
                    System.out.println("终止超期订单失败，订单编号"+tmpId);
                }else{
                    System.out.println("终止超期订单，订单编号"+tmpId);
                }
            }
        }
    }



}
